package com.hjxr.quota.define.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.hjxr.common.core.controller.BaseController;
import com.hjxr.common.utils.StringUtils;
import com.hjxr.quota.define.domain.DimDetail;
import com.hjxr.quota.define.domain.Quota;
import com.hjxr.quota.define.domain.QuotaFormula;
import com.hjxr.quota.define.domain.QuotaSource;
import com.hjxr.quota.define.domain.vo.IdNameVo;
import com.hjxr.quota.define.dto.DimensionVo;
import com.hjxr.quota.define.dto.QuotaCreateDto;
import com.hjxr.quota.define.service.DimDetailService;
import com.hjxr.quota.define.service.DimRelationService;
import com.hjxr.quota.define.service.QuotaService;
import com.hjxr.quota.define.service.QuotaSourceService;
import com.hjxr.quota.util.PageUtil;
import com.hjxr.quota.datasource.controller.BaseForm;
import com.hjxr.quota.datasource.domain.QuotaDimension;
import com.hjxr.sysadmin.security.service.TokenService;
import com.hjxr.sysadmin.system.domain.SysDictData;
import com.hjxr.sysadmin.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指标定义控制层
 *
 * @author ma
 * @date 2020/9/14 - 15:46
 **/
@RestController
@RequestMapping("/quotaDefine")
@Api(tags = "指标定义接口")
public class QuotaController extends BaseController {

    @Autowired
    private QuotaService quotaService;
    @Autowired
    private QuotaSourceService quotaSourceService;
    @Autowired
    private DimDetailService dimDetailService;
    @Autowired
    private DimRelationService dimRelationService;
    @Resource
    private TokenService tokenService;
    @Resource
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("分页查询所有数据")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", dataType = "String", name = "current", value = "当前页", defaultValue = "1", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "size", value = "一页大小", defaultValue = "10", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "Boolean", name = "ifCount", value = "是否查询总数", defaultValue = "true"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "ascs", value = "升序字段，多个用逗号分隔"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "descs", value = "降序字段，多个用逗号分隔")
            })
    public R<IPage<Quota>> selectAll() {
        BaseForm form = new BaseForm();
        QueryWrapper<Quota> query = (QueryWrapper<Quota>) form.pageQueryWrapperCustom(form.getParameters(), new QueryWrapper<Quota>());
        return success(quotaService.page(form.getPlusPagingQueryEntity(), query));
    }

    @PostMapping("/createQuota")
    @ApiOperation("生成指标")
    private R<Boolean> createQuota(@RequestBody List<QuotaCreateDto> quotaCreateDtos) {
        return success(quotaService.createQuota(quotaCreateDtos));
    }

    @PostMapping("/saveCustomQuota")
    @ApiOperation("生成自定义预指标")
    private R saveCustomQuota(@RequestBody QuotaCreateDto quotaCreateDto) {
        return quotaService.saveCustomQuota(quotaCreateDto);
    }

    @PostMapping("/saveQuota")
    @ApiOperation("新增数据库来源指标")
    private R<Boolean> saveQuota(@RequestBody QuotaCreateDto quotaCreateDto) {
        return success(quotaService.saveQuota(quotaCreateDto));
    }

    @PostMapping("/createDimension")
    @ApiOperation("生成维度")
    private R<Boolean> createDimension(HttpServletRequest request, @RequestBody List<DimensionVo> dimensionVos) {
        String username = tokenService.getLoginUser(request).getUsername();
        if (StringUtils.isBlank(username)) {
            return failed("请先登录");
        }
        return success(quotaService.createDimension(dimensionVos, username));
    }

    /**
     * 指标表操作
     */
    @GetMapping("/selectQuota")
    @ApiOperation("根据id查询指标表单条数据")
    public R<Quota> selectQuotaById(@RequestParam(required = true) Integer id) {
        return success(quotaService.getById(id));
    }

    @GetMapping("/getList")
    @ApiOperation("获取指标表数据列表")
    public R<PageUtil> getList(@RequestParam Map<String, Object> maps) {
        return success(quotaService.getList(maps));
    }

    @GetMapping("/getByTheme")
    @ApiOperation("根据主题id查询指标表")
    public R<PageUtil> queryList(@RequestParam Map<String, Object> maps) {
        return success(quotaService.queryList(maps));
    }

    @PutMapping("/updateQuota")
    @ApiOperation("根据id修改指标表")
    public R<Boolean> updateQuota(@RequestBody Quota quota) {
        return success(quotaService.updateById(quota));
    }

    @DeleteMapping("/deleteQuota")
    @ApiOperation("根据id删除指标表")
    public R<Boolean> deleteQuota(@RequestParam(required = true) Integer id) {
        return success(quotaService.removeById(id));
    }

    @DeleteMapping("/deleteQuotaByids")
    @ApiOperation("根据id列表批量删除指标表")
    public R<Boolean> deleteQuotaByids(@RequestBody List<Integer> ids) {
        return success(quotaService.removeByIds(ids));
    }

    /**
     * 指标来源表操作
     */
    @GetMapping("/querySource")
    @ApiOperation("根据ID查询指标来源表")
    public R<QuotaSource> querySource(@RequestParam(required = true) Integer id) {
        return success(quotaSourceService.getById(id));
    }

    /**
     * 维度详细-字段关联表操作
     */
    @PostMapping("/insertDetail")
    @ApiOperation("插入维度详细-字段关联表")
    public R<Boolean> insertDetail(@RequestBody DimDetail dimDetail) {
        return success(dimDetailService.save(dimDetail));
    }


    /**
     * 维度与指标关系表操作
     */
    @PostMapping("/quotaModeling")
    @ApiOperation(value = "指标建模", notes = "插入维度与指标关系表")
    public R<Boolean> insertValue(@RequestBody Map<Integer, List<IdNameVo>> parms) {
        return success(dimRelationService.insert(parms));
    }

    @GetMapping("/queryDim/{id}")
    @ApiOperation(value = "根据指标id查询维度信息列表")
    public R<List<QuotaDimension>> queryDimByQId(@PathVariable Integer id) {
        return success(dimRelationService.queryDim(id));
    }

    @PostMapping("/getQuotaAndValue")
    @ApiOperation(value = "根据指标id查询指标信息和指标值")
    public R<Map<String, Object>> getQuotaAndValue(@RequestBody Map<String, Object> params) {
        return success(quotaService.getQuotaAndValue(params));
    }


    @GetMapping("/queryQuotaType")
    @ApiOperation("字典管理查询指标类型")
    public R<List<Map<String, String>>> queryQuotaType() {
        List<Map<String, String>> list = new ArrayList<>();
        List<SysDictData> sysDictDataList = iSysDictTypeService.selectDictDataByType("sys_quota_type");
        for (SysDictData sysDictData : sysDictDataList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", sysDictData.getDictValue());
            map.put("value", sysDictData.getDictLabel());
            list.add(map);
        }
        return success(list);
    }

    @GetMapping("/queryComputeType")
    @ApiOperation("字典管理查询计算值类型")
    public R<List<Map<String, String>>> queryComputeType() {
        List<Map<String, String>> list = new ArrayList<>();
        List<SysDictData> sysDictDataList = iSysDictTypeService.selectDictDataByType("sys_compute_type");
        for (SysDictData sysDictData : sysDictDataList) {
            Map<String, String> map = new HashMap<>();
            map.put("value", sysDictData.getDictValue());
            map.put("label", sysDictData.getDictLabel());
            list.add(map);
        }
        return success(list);
    }

    @ApiOperation(value = "查询指标详情")
    @GetMapping("/getQuotaDetail/{id}")
    public R<Map<String, String>> getQuotaDetail(@PathVariable("id") Integer id) {
        return success(quotaService.getQuotaDetail(id));
    }

    @ApiOperation(value = "查询指标源头 仅指标生成指标可用")
    @GetMapping("/lookForSource/{id}")
    public R<List<QuotaFormula>> lookForSource(@PathVariable("id") Integer id) {
        return success(quotaService.lookForSource(id));
    }

    @ApiOperation(value = "执行自定义sql")
    @GetMapping("/executeSql")
    public R executeSql(@RequestParam Map<String,String>params) {
        return quotaService.executeSql(params);
    }

}
